package site.param.util;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import site.param.bean.MonitorBean;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2017/1/3.
 */
public class MonitorUtil {
    public static MonitorBean systemInfo() {

        MonitorBean monitorBean = new MonitorBean();
        Sigar sigar = new Sigar();
        try {
            Runtime r = Runtime.getRuntime();
            Mem mem = sigar.getMem();
            Properties props = System.getProperties();

            InetAddress addr = InetAddress.getLocalHost();
            if (addr != null)
                monitorBean.setIp(addr.getHostAddress());
            Map<String, Double> desk= fileDesk();
            Map<String, String> map = System.getenv();
            monitorBean.setComputerName(map.get("COMPUTERNAME"));
            monitorBean.setJavaVersion(props.getProperty("java.version"));
            monitorBean.setMemTotal((mem.getTotal()/1024L/1024L)+"");
            monitorBean.setMemUsed((mem.getUsed()/1024L/1024L)+"");
            monitorBean.setCpu(cpu()+"");
            monitorBean.setDeskTotal(desk.get("total")+"");
            monitorBean.setDeskUsed((desk.get("total")-desk.get("avail"))+"");

        } catch (Exception e) {

        }
        return monitorBean;
    }


    private static int cpu() throws SigarException {
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuList[] = null;
        cpuList = sigar.getCpuPercList();
        double cpu=0;
        for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
            cpu+=cpuList[i].getCombined();
        }
        return (int)cpu/cpuList.length;

    }
    private static Map<String,Double> fileDesk() throws Exception {
        Sigar sigar = new Sigar();
        FileSystem fslist[] = sigar.getFileSystemList();
        double total=0;
        double avail=0;
        Map<String,Double> map=new HashMap<>();
        if (fslist==null||fslist.length==0){
            return map;
        }
        for (int i = 0; i < fslist.length; i++) {
            FileSystem fs = fslist[i];
            FileSystemUsage usage = null;
            usage = sigar.getFileSystemUsage(fs.getDirName());
            switch (fs.getType()) {
                case 0: // TYPE_UNKNOWN ：未知
                    break;
                case 1: // TYPE_NONE
                    break;
                case 2: // TYPE_LOCAL_DISK : 本地硬盘
                    total+=usage.getTotal();
                    avail+=usage.getAvail();
                    break;
                case 3:// TYPE_NETWORK ：网络
                    break;
                case 4:// TYPE_RAM_DISK ：闪存
                    break;
                case 5:// TYPE_CDROM ：光驱
                    break;
                case 6:// TYPE_SWAP ：页面交换
                    break;
            }

        }
        total=total/fslist.length/1024/1024;
        avail=avail/fslist.length/1024/1024;
        map.put("avail",avail);
        map.put("total",total);
        return map;
    }

    public static void main(String[] args) {
         MonitorBean  m=systemInfo();

        System.out.println("args = [" + args + "]");
    }

}
